package com.altio.swing;


import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;

import javax.swing.JComponent;

/**
 * Box drawn when the user drags the mouse across a component. Best used in
 * conjunction with the {@link DragSelectListener} class which will map this
 * components bounds to the users mouse actions.
 * 
 * @author tomm
 * 
 */
public class DragBox extends JComponent {
  public DragBox() {
    setForeground(Color.blue);
  }

  /**
   * Paint ourselves
   * 
   * @param g Graphics context
   */
  protected void paintComponent(final Graphics g) {
    // TODO prototype painting code
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g;

    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    // Initialise a dashed line
    float[] intervals = new float[] { 15 };
    g2d.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 50, intervals, 0));

    final int DEF_X = 3;
    final int DEF_Y = 3;
    final int ADJUST_X = 7;
    final int ADJUST_Y = 7;
    final int PAINT_ADJUST = 4;
    //Rectangle originalClip = new Rectangle(g.getClipBounds());
    g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
    g2d.setColor(getBackground());
    g2d.fillRect(DEF_X, DEF_Y, getWidth() - ADJUST_X, getHeight() - ADJUST_Y);
    g2d.setColor(getForeground());
    g2d.setPaint(new GradientPaint(new Point(0, 0), new Color(200, 200, 255), new Point(getWidth() / PAINT_ADJUST, getHeight() / PAINT_ADJUST), getForeground()));
    g2d.drawRect(DEF_X, DEF_Y, getWidth() - ADJUST_X, getHeight() - ADJUST_Y);

  }
}
